package webServlets;

import domain.Account;
import service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RegisterSuccessfulServlet extends HttpServlet {
    private static final String SIGN_ON = "/WEB-INF/account/signonForm.jsp";
    private static final String ERROR = "/WEB-INF/common/Error.jsp";
    private Account account;
    private AccountService accountService;
    private String username;
    private String password;
    private String repeatedPassword;
    private String valistr;
    private String firstName;
    private String lastName;
    private String email;
    private String phone;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private String country;
    private String languages;
    private String categories;
    private boolean listOption;
    private boolean bannerOption;


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        username = request.getParameter("username");
        password = request.getParameter("password");
        repeatedPassword = request.getParameter("repeatedPassword");
        valistr = request.getParameter("valistr");
        firstName = request.getParameter("firstName");
        lastName = request.getParameter("lastName");
        email = request.getParameter("email");
        phone = request.getParameter("phone");
        address1 = request.getParameter("address1");
        address2 = request.getParameter("address2");
        city = request.getParameter("city");
        state = request.getParameter("state");
        zip = request.getParameter("zip");
        country = request.getParameter("country");
        languages = request.getParameter("languagePreference");
        categories = request.getParameter("favouriteCategoryId");

        if(request.getParameter("listOption") != null){
            listOption =  true;
        }else{
            listOption = false;
        }

        if(request.getParameter("bannerOption") != null){
            bannerOption =  true;
        }else{
            bannerOption = false;
        }

        HttpSession session = request.getSession();
        List<String> lang = new ArrayList<>();
        lang.add("English");
        lang.add("Chinese");
        session.setAttribute("languages",languages);

        List<String> cate = new ArrayList<>();
        cate.add("FISH");
        cate.add("DOGS");
        cate.add("REPTILES");
        cate.add("CATS");
        cate.add("BIRDS");
        session.setAttribute("categories",categories);


        //验证码不为空
        if (!valistr.equals("")) {
            //验证码验证
            String text = ValiImageServlet.text;
            String text1 = text.toUpperCase();//全部为大写
            String text2 = text.toLowerCase();//全部为小写
            if (valistr.matches(text1) || valistr.matches(text2) || valistr.matches(text)) {
                if(password.equals(repeatedPassword)) {
                    account = new Account();

                    account.setUsername(username);
                    account.setPassword(password);
                    account.setFirstName(firstName);
                    account.setLastName(lastName);
                    account.setEmail(email);
                    account.setPhone(phone);
                    account.setAddress1(address1);
                    account.setAddress2(address2);
                    account.setCity(city);
                    account.setState(state);
                    account.setZip(zip);
                    account.setCountry(country);
                    account.setLanguagePreference(languages);
                    account.setFavouriteCategoryId(categories);
                    account.setListOption(listOption);
                    account.setBannerOption(bannerOption);

                    AccountService accountService = new AccountService();
                    accountService.insertAccount(account);
                    request.getRequestDispatcher(SIGN_ON).forward(request, response);
                }
            } else {
                request.getRequestDispatcher(ERROR).forward(request, response);
            }
        } else {
            request.getRequestDispatcher(ERROR).forward(request, response);
        }



    }
}
